package com.wanmait.recruit.controller;


import com.github.pagehelper.PageInfo;
import com.wanmait.recruit.pojo.Industry;
import com.wanmait.recruit.pojo.Person;
import com.wanmait.recruit.pojo.Position;
import com.wanmait.recruit.pojo.PositionType;
import com.wanmait.recruit.service.IndustryService;
import com.wanmait.recruit.service.PersonService;
import com.wanmait.recruit.service.PositionService;
import com.wanmait.recruit.service.PositionTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.*;
import java.util.List;

@Controller
@RequestMapping("/web")
public class WebController {
    @Resource
    private IndustryService industryService;
    @Resource
    private PositionTypeService positionTypeService;
    @Resource
    private PositionService positionService;
    @Resource
    private PersonService personService;

    @RequestMapping("/checkCheckCode")
    public void checkCheckCode(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        String myCode = request.getParameter("myCode");
        String checkCode = (String) request.getSession().getAttribute("checkCode");
        PrintWriter pw = response.getWriter();
        if(!myCode.equals(checkCode)){
            pw.print(0);
        }
        else{
            pw.print(1);
        }
    }
    @RequestMapping("/getCheckCode")
    public void getcheckcode(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        BufferedImage huaBu = new BufferedImage(60,30,BufferedImage.TYPE_INT_RGB);
        //huaBu默认黑色
        Graphics huaBi;
        huaBi = huaBu.getGraphics();
        //画笔默认白色

        huaBi.fillRect(0, 0, 60, 30);
        //画白色填充的矩形
        huaBi.setColor(Color.BLACK);

        Random random = new Random();
        String checkCode = "";//存放验证码
        while(true)
        {
            int data = random.nextInt(75)+48;//48--122
            if((data>=48&&data<=57)||(data>=65&&data<=90)||(data>=97&&data<=122))
            {
                checkCode = checkCode+(char)data;
            }
            if(checkCode.length()==4)
            {
                break;
            }
        }
        //生成验证码

        //将验证码保存到Session
        HttpSession session = request.getSession();
        session.setAttribute("checkCode", checkCode);

        //画验证码
        huaBi.drawString(checkCode, 10, 20);
        //将验证码画到画布

        //将画布发送到客户端
        OutputStream os = response.getOutputStream();
        //os向浏览器发送数据的字节流
        ImageIO.write(huaBu, "jpg", os);

    }

    @RequestMapping("/checkEmailSuccess")
    public void checkEmailSuccess(){}

    @RequestMapping("/checkEmailFail")
    public void checkEmailFail(){}

    @RequestMapping("/index2")
    public void index2(){}

    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView modelAndView=new ModelAndView();

        java.util.List<Industry> industries = industryService.selectFindAll();
        modelAndView.addObject("industries",industries);
        Map<Integer, java.util.List<PositionType>> positionTypeMap=new HashMap<>();
        industries.forEach(industry -> {
            java.util.List<PositionType> allByIndustryId = positionTypeService.findAllByIndustryId(industry.getId());
            positionTypeMap.put(industry.getId(),allByIndustryId);
        });
        modelAndView.addObject("positionTypeMap",positionTypeMap);

        ArrayList<PositionType> positionTypes = positionTypeService.selectFindAll();
        Map<Integer, java.util.List<Position>> positionMap=new HashMap<>();
        positionTypes.forEach(positionType -> {
            List<Position> allByPositionTypeId=positionService.selectALLByPositionTypeId(positionType.getId());
            positionMap.put(positionType.getId(), allByPositionTypeId);
        });
        modelAndView.addObject("positionMap",positionMap);

        modelAndView.setViewName("/web/index");
        return modelAndView;
    }
    @RequestMapping("blog")
    public ModelAndView blog(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("/web/blog/blog");

        PageInfo<Person> all = personService.findAll();
        modelAndView.addObject("all",all);

        return modelAndView;
    }
}
